package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @ClassName AccordionDemo
 * @Description 手风琴折叠
 * @Author wangss
 * @date 2020.05.19 00:42
 * @Version 1.0
 */
public class AccordionDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();

        TitledPane titledPane1 = new TitledPane("title1", new Button("button1"));
        TitledPane titledPane2 = new TitledPane();
        titledPane2.setText("title2");
        titledPane2.setContent(new Button("button2"));

        TitledPane titledPane3 = new TitledPane();
        titledPane3.setText("title3");
        HBox hBox = new HBox();
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        hBox.getChildren().addAll(button3, button4, button5);
        titledPane3.setContent(hBox);
        // 设置图片
        Image image = new Image("image/icon1.png", 30d, 30d, true, true);
        titledPane3.setGraphic(new ImageView(image));

        // 相当于一个面板
        Accordion accordion = new Accordion();
        accordion.getPanes().addAll(titledPane1, titledPane2, titledPane3);

        anchorPane.getChildren().addAll(accordion);

        Scene scene = new Scene(anchorPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        // 监听变化
        accordion.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
            @Override
            public void changed(ObservableValue<? extends TitledPane> observable, TitledPane oldValue, TitledPane newValue) {
                if (newValue != null) {
                    System.out.println(newValue.getText());
                }
            }
        });
    }
}
